package me.khrystal.rxnews.app;

import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.socks.library.KLog;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import com.zhy.changeskin.SkinManager;

import de.greenrobot.dao.query.QueryBuilder;
import me.khrystal.rxnews.BuildConfig;
import me.khrystal.rxnews.common.Constant;
import me.khrystal.rxnews.greendao.DaoMaster;
import me.khrystal.rxnews.greendao.DaoSession;

/**
 * 全局Application对象
 *
 * @FileName: me.khrystal.rxnews.app.App.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-03-03 10:33
 */
public class App extends Application{

    private RefWatcher mRefWatcher;

    private DaoSession mDaoSession;

    private static Context mApplicationContext;

    @Override
    public void onCreate() {
        super.onCreate();
        SkinManager.getInstance().init(this);
//        init skin change manager if OOM this will toast notification
        mRefWatcher = LeakCanary.install(this);
//        init DB
        setupDatabase();
        mApplicationContext = this;
        KLog.init(BuildConfig.DEBUG);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }

    /**
     * get Memory monitor
     * @param context
     * @return
     */
    public static RefWatcher getRefWatcher(Context context){
        App application = (App)context.getApplicationContext();
        return application.mRefWatcher;
    }

    /**
     * TODO it's must be modify!!
     */
    private void setupDatabase() {
        DaoMaster.DevOpenHelper helper =
                new DaoMaster.DevOpenHelper(this, Constant.DB_NAME,null);
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        mDaoSession = daoMaster.newSession();
        QueryBuilder.LOG_SQL = BuildConfig.DEBUG;
        QueryBuilder.LOG_VALUES = BuildConfig.DEBUG;
    }

    /**
     * get DAO Session
     * @return
     */
    public DaoSession getDaoSession(){
        return mDaoSession;
    }

    /**
     * get ApplicationContext
     * @return
     */
    public static Context getContext(){
        return mApplicationContext;
    }
}
